/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.uml21;

import java.io.File;

/*import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
*/
import org.mitre.xmltransform.XPathNamespaceContextImpl;
import org.mitre.xmltransform.XmlInputDocument;
//import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Uml21InputDocument extends XmlInputDocument {
	public Uml21InputDocument(File pInputFile) {
		super(pInputFile);
	}

	protected void addNamespaces(XPathNamespaceContextImpl pNamespaces) {
		pNamespaces.addPrefix("uml", "http://schema.omg.org/spec/UML/2.1.1");
		pNamespaces.addPrefix("xmi", "http://schema.omg.org/spec/XMI/2.1");
		pNamespaces.addPrefix("RTtimeModeling_profile", "http://SPT.RTtimeModeling/schemas/RTtimeModeling_profile");
		pNamespaces.addPrefix("RTschedulabilityModeling_profile", "http://SPT.RTschedulabilityModeling/schemas/RTschedulabilityModeling_profile");
		pNamespaces.addPrefix("PAprofile_profile", "http://SPT.PAprofile/schemas/PAprofile_profile");
	}
	
	protected void updateNamespaces(XPathNamespaceContextImpl pNamespaces) {
		// Rhapsody namespace workaround
		// this will only update the namespaces if it finds the prefixes used here and then
		// it updates them to match the actual namespace used
		// therefore, this hack to handle the random namespaces created by Rhapsody
		// should not affect any other tools; otherwise, I would create
		// a subclass called RhapsodyUML21InputDocument. Of course, then the transform
		// would need a way to specify which type of document it is receiving or
		// auto-detect it itself. This is much simpler for now.
		NamedNodeMap namespaces = getNamespaceAttributes();
		if (namespaces.getNamedItem("xmlns:RTtimeModeling_profile") != null) {
			pNamespaces.addPrefix("RTtimeModeling_profile", namespaces.getNamedItem("xmlns:RTtimeModeling_profile").getNodeValue());
		}
		if (namespaces.getNamedItem("xmlns:RTschedulabilityModeling_profile") != null) {
			pNamespaces.addPrefix("RTschedulabilityModeling_profile", namespaces.getNamedItem("xmlns:RTschedulabilityModeling_profile").getNodeValue());
		}
		if (namespaces.getNamedItem("xmlns:PAprofile_profile") != null) {
			pNamespaces.addPrefix("PAprofile_profile", namespaces.getNamedItem("xmlns:PAprofile_profile").getNodeValue());
		}
	}

	public String buildNodeName(Node pContext, String pFixedPrefix) {
		String prefix, namespace, localName, nodeName, xmiId, resultPrefix, nodeId;
		NodeList allNodes;
		Node node;
		
		//String name = this.getInputValue(pContext, "@name");
		//if (name == null || name.length() == 0) {
			
			namespace = pContext.getNamespaceURI();
			if (namespace == null) {
				prefix = null;
			} else {
				prefix = this.getNamespacePrefix(namespace);
			}
			localName = pContext.getLocalName();
			nodeName = ((prefix != null && prefix.length() > 0) ? prefix + ":" : "") + localName; 
			allNodes = this.getInputNodes("//" + nodeName);

			resultPrefix = (pFixedPrefix != null) ? pFixedPrefix : localName;
			nodeId = this.getInputValue(pContext, "@xmi:id");
			for (int i = 0; i < allNodes.getLength(); i++) {
				node = allNodes.item(i);
				xmiId = this.getInputValue(node, "@xmi:id");
				if (xmiId.equals(nodeId)) {
					return resultPrefix + (i+1);
				}
			}
			return resultPrefix + "_ID" + nodeId;
		//}
		//return name;
	}
}
